package jj.fly.activiti.leavebill.controller;

import jj.fly.activiti.leavebill.domain.Employee;
import jj.fly.activiti.leavebill.domain.LeaveBill;
import jj.fly.activiti.leavebill.domain.custom.LeaveBillCustom;
import jj.fly.activiti.leavebill.service.ILeaveBillService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Author: jiangjun
 * Date: 2018/5/9
 * Time: 下午2:14
 * Description:
 */
@Controller
@RequestMapping("/leaveBill")
public class LeaveBillController {

    @Resource
    private ILeaveBillService leaveBillService;

    @GetMapping("/leaveBillUI")
    public String leaveBillUI(HttpServletRequest request, Model model){
        Employee currentUser = (Employee) request.getSession().getAttribute("current_user");
        List<LeaveBillCustom> leaveBillList = leaveBillService.getDataList(currentUser.getId());
        model.addAttribute("leaveBillList", leaveBillList);
        return "leaveBill/list";
    }

    @GetMapping("/editUI")
    public String toEditUI(@RequestParam(value = "id", required = false) Integer id,
                           Model model){
        if(id != null){
            LeaveBill leaveBill = leaveBillService.getDataById(id);
            model.addAttribute("leaveBill", leaveBill);
        }
        return "leaveBill/edit";
    }

    @PostMapping("/save")
    public String save(HttpServletRequest  request,
                       LeaveBill leaveBill){
        if(leaveBill.getId() == null){
            // insert
            Employee currentUser = (Employee) request.getSession().getAttribute("current_user");

            leaveBill.setState(0);
            leaveBill.setEmployeeId(currentUser.getId());
        }
        leaveBillService.save(leaveBill);
        return "redirect:/leaveBill/leaveBillUI";
    }

    @GetMapping("/{id}/delete")
    public String delete(@PathVariable("id") Integer id){
        leaveBillService.deleteById(id);
        return "redirect:/leaveBill/leaveBillUI";
    }

}
